<?php
namespace app\common\logic;

use app\common\component\user_wallet\UserWalletService;
use app\common\component\user_wallet_freeze\UserWalletFreezeModel;
use app\common\component\user_wallet_freeze\UserWalletFreezeService;
use app\common\component\user_wallet_log\UserWalletLogService;

class UserWalletFreeze {
	/**
	 * @description 冻结金钱
	 * @value user_id 用户ID
	 * @value money 金钱
	 * @value interest 判断是否有利息
	 * @value remark 备注
	 * @value monry_type 钱包类型
	 */
	public function doFreeze($user_id, $money, $interest = false, $remark = '', $money_type = 1, $end_time = null) {
		$data = [];
		$data['user_id'] = $user_id;
		$data['money'] = $money;
		if($interest) {
			//if(\think\Session::get('is_vip')) {
				$data['interest'] = intval(getParam('HELP_TO_VIP_MONEY_INCOME') / 100 * $money);
			//} else {
				//$data['interest'] = getParam('HELP_TO_MONEY_INCOME') / 100 * $money;
			//}
		}
		$data['begin_time'] = $data['create_time'] = time();
		if($end_time) {
			$data['end_time'] = $end_time;
		} else {
			$data['end_time'] = $data['begin_time'] +  (getParam('MONEY_FREEZE_TIME') * 24 * 60 * 60);
		}

		$data['money_type'] = $money_type;
		$data['remark'] = $remark;
		$data['is_freeze'] = 0;

		return (new UserWalletFreezeService()) -> create($data);
	}

	/**
	 * @description 解冻金钱
	 */
	public function doThaw() {
		$userWalletFreezes = UserWalletFreezeModel::where('is_freeze = 0 AND end_time <= ' . time()) -> select();

		if(empty($userWalletFreezes)) {
			return false;
		}

		$userWalletService = new UserWalletService();
		$userWalletLogService = new UserWalletLogService();
		\think\Db::startTrans();

		try {
			foreach($userWalletFreezes as $userWalletFreeze) {
				$userWalletFreeze -> is_freeze = 1;
				if($userWalletFreeze -> save()) {

					if($userWalletFreeze -> money > 0) {
						$resultUserWalletLog = $userWalletLogService -> logUserId(4, 1, $userWalletFreeze -> money, (int)$userWalletFreeze -> user_id, '冻结本金转入钱包', 1);
						if($resultUserWalletLog['code'] != 0) {
							throw new \Exception($resultUserWalletLog['message']);
						}

						$resultUserWallet = $userWalletService -> setIncMoney($userWalletFreeze -> money, $userWalletFreeze -> user_id);
						if($resultUserWallet['code'] != 0) {
							throw new \Exception($resultUserWallet['message']);
						}
					}

					if($userWalletFreeze -> interest > 0) {
						$resultUserWalletLog = $userWalletLogService -> logUserId(5, 1, $userWalletFreeze -> interest, (int)$userWalletFreeze -> user_id, '冻结利息转入钱包', 1);
						if($resultUserWalletLog['code'] != 0) {
							throw new \Exception($resultUserWalletLog['message']);
						}

						$resultUserWallet = $userWalletService -> setIncMoney($userWalletFreeze -> interest, $userWalletFreeze -> user_id);
						if($resultUserWallet['code'] != 0) {
							throw new \Exception($resultUserWallet['message']);
						}
					}

				} else {
					\think\Log::error('user_id: ' . $userWalletFreeze -> user_id . ', 解冻失败!');
				}
			}
			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			\think\Log::error($e -> getMessage());
		}
	}
}
?>